/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.bean;

import org.ipti.br.tag.bo.ActorBO;
import org.ipti.br.tag.bo.LoginBO;
import org.ipti.br.tag.entity.Actor;
import org.ipti.br.tag.entity.Login;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;
import org.ipti.br.tag.util.EncryptionUtil;

/**
 *
 * @author israel
 */
@ManagedBean(name = "loginBean")
@ViewScoped
public class LoginBean extends AppBaseBean {

    @Inject
    private LoginBO loginBO;
    @Inject
    private ActorBO actorBO;
    @ManagedProperty("#{sessionBean}")
    private SessionBean sessionBean;
    private Login login;
    private Long actorID;
    private List<SelectItem> actorList;

    public LoginBean() {
    }

    public void loginFinal() {
        sessionBean.setLoggedActor(actorBO.find(actorID));
        redirect("/educational/home/home.xhtml");
    }

    public void preLogin() {
        actorList = new ArrayList<SelectItem>();
        //Long personID = loginBO.validateLogin(login);
        List<Login> loginList = loginBO.searchLogins(login.getUserName());
        if (loginList.size() <= 1) {
            if (!loginList.isEmpty() && EncryptionUtil.auth(login.getPassword(), loginList.get(0).getPassword())) {
                for (Actor a : actorBO.actorActivatedPerPerson(loginList.get(0).getPerson().getId())) {
                    actorList.add(new SelectItem(a.getId(), a.getPersonage().getName() + " - " + a.getUnity().getName()));
                }
                if (actorList.isEmpty()) {
                    addInfoMessage("loginForm:loginGrid", null, "Não há atores disponíveis ou a hora do servidor está errada");
                }
                if (actorList.size() == 1) {
                    actorID = (Long) actorList.get(0).getValue();
                    loginFinal();
                }
            } else {
                addErrorMessage("loginForm:loginGrid", null, "Login inválido!");
            }
        } else {
            addErrorMessage("loginForm:loginGrid", null, "Conflito de logins. Contate o administrador.");
        }
    }

    private void newLogin() {
        login = new Login();
    }

    /*
     * Getters and Setters
     */
    public ActorBO getActorBO() {
        return actorBO;
    }

    public void setActorBO(ActorBO actorBO) {
        this.actorBO = actorBO;
    }

    public LoginBO getLoginBO() {
        return loginBO;
    }

    public void setLoginBO(LoginBO loginBO) {
        this.loginBO = loginBO;
    }

    public SessionBean getSessionBean() {
        return sessionBean;
    }

    public void setSessionBean(SessionBean sessionBean) {
        this.sessionBean = sessionBean;
    }

    public Login getLogin() {
        if (login == null) {
            newLogin();
        }
        return login;
    }

    public void setLogin(Login login) {
        this.login = login;
    }

    public Long getActorID() {
        return actorID;
    }

    public void setActorID(Long actorID) {
        this.actorID = actorID;
    }

    public List<SelectItem> getActorList() {
        if (actorList == null) {
            actorList = new ArrayList<SelectItem>();
        }
        return actorList;
    }

    public void setActorList(List<SelectItem> actorList) {
        this.actorList = actorList;
    }
}
